<template>
  <div class="main-menu">
    <div class="logo">
      <img class="img" src="@/assets/image/logo.svg" alt="logo" />
      <h2 class="title" v-show="!isFold">古树信息管理系统</h2>
    </div>
    <div class="menu">
      <!-- 整个菜单 -->
      <el-menu text-color="#b7bdc3" active-text-color="#fff" background-color="#001529" :collapse=isFold
        :default-active="defaultAction">
        <template v-for="item in userMenus" :key="item.id">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <el-icon>
                <component :is="item.icon" />
              </el-icon>
              <span>{{ item.menuName }}</span>
            </template>
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item :index="subitem.id + ''" @click="handleItemClick(subitem.path)">
                <el-icon>
                  <component :is="subitem.icon" />
                </el-icon>
                {{ subitem.menuName }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>

        <!-- 可以有子菜单，并且是可以展开的 -->
        <!--  <el-sub-menu index="1">
          <template #title>
            <el-icon>
              <Tickets />
            </el-icon>
            <span>古树管理</span>
          </template> -->
        <!-- 对子菜单进行分组，但是不能展开 -->
        <!--  <el-menu-item-group>
            <el-menu-item index="1-1">古树信息</el-menu-item>
            <el-menu-item index="1-2" @click="handleItemClick('/main/map')">古树地图</el-menu-item>
          </el-menu-item-group>
        </el-sub-menu> -->

      </el-menu>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { useRoute, useRouter } from 'vue-router'

import { computed } from 'vue'
import { userLoginStore } from '@/store/login/login'
import { mapPathToMenu } from '@/utils/map-menu'



export interface MenuItem {
  id: number;
  menuName: string;
  path: string;
  icon: string;
  children: MenuItem[];
}

const menuStore = userLoginStore()
const userMenus = menuStore.menu as MenuItem[]
console.log('userMenu' + userMenus)


//接收父组件传参
defineProps({
  isFold: {
    type: Boolean,
    default: false,
  },


});

const router = useRouter()

//监听item点击
function handleItemClick(item: any) {
  router.push(item)
}
// 默认选中菜单

const route = useRoute()

//计算当前路径有没有变化，
const defaultAction = computed(() => {
  //  根据当前路径，返回对应的菜单项

  const pathMenu = mapPathToMenu(route.path, userMenus)
  if (!pathMenu) {
    // 如果没有匹配到路径，则默认选中第一个菜单项
    return userMenus[0].id + ''
  } else {
    return pathMenu.id + ''
  }

}
)



</script>

<style lang="scss" scoped>
.main-menu {
  height: 100%;
  background-color: #2b2f3a;
}

.logo {
  display: flex; // 使用flex布局以实现内容的灵活排列
  align-items: center; // 垂直居中对齐flex项目
  justify-content: flex-start; // 从左到右对齐flex项目，保持默认起始位置对齐
  padding: 12px 10px 8px 10px; // 设置内边距，分别为上、右、下、左边距
  height: 40px; // 设置容器的高度
  overflow: hidden; // 隐藏溢出内容，保持内容不超出显示区域
  flex-direction: row; // 设置flex方向为从左到右的行排列

  .img {
    height: 100%;
    margin: 0 10px
  }

  .title {
    margin-left: 10px;
    color: white;
    font-size: 16px;
    font-weight: 700;
    white-space: nowrap;
  }
}

.el-menu {
  border-right: none;
  user-select: none;
}

.el-sub-menu {

  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }

  .el-menu-item:hover {
    color: #fff;
  }

  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
